﻿using DevExpress.XtraLayout.Utils;
using HealthExaminationSystem.Enumerations.Helpers;
using Sw.Hospital.HealthExaminationSystem.ApiProxy.ItemInfo;
using Sw.Hospital.HealthExaminationSystem.Application.ItemInfo.Dto;
using Sw.Hospital.HealthExaminationSystem.Common.Bases;
using Sw.Hospital.HealthExaminationSystem.Common.Helpers;
using Sw.Hospital.HealthExaminationSystem.Common.UserCache;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sw.Hospital.HealthExaminationSystem.CusReviews
{
    public partial class frmCriManage : UserBaseForm
    {
        private List<ItemInfoSimpleDto> CacheItemInfoSimpleDto = new List<ItemInfoSimpleDto>();
        ItemInfoAppService itemInfoService = new ItemInfoAppService();
        public frmCriManage()
        {
            InitializeComponent();
        }

        private void frmCriManage_Load(object sender, EventArgs e)
        {
              CacheItemInfoSimpleDto = DefinedCacheHelper.GetItemInfos();
            lookUpitem.Properties.DataSource = CacheItemInfoSimpleDto;

            lookUpEdit2.Properties.DataSource = VisitgradeHelper.GetModels();
            var xingbieList = SexHelper.GetSexModelsForItemInfo();
            lue_xingbie.Properties.DataSource = xingbieList;

            //异常状态
            checkedComYCZT.Properties.DataSource = SymbolHelper.GetLabelingStateModels();
            checkedComYCZT.Properties.DisplayMember = "Display";
            checkedComYCZT.Properties.ValueMember = "Id";

        }

        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(lookUpitem.EditValue?.ToString()))
            {
                var itemID = (Guid)lookUpitem.EditValue;
                var itemg = CacheItemInfoSimpleDto.FirstOrDefault(p => p.Id == itemID);
                if (itemg != null)
                {
                    if (itemg.moneyType == 1 || itemg.moneyType == 2)
                    {

                        layoutCon.Visibility = LayoutVisibility.Never;
                        layoutMin.Visibility = LayoutVisibility.Always;
                        layoutMax.Visibility = LayoutVisibility.Always;
                        var jieguoList = ResultJudgementStateHelper.GetResultNumStateModels();
                        lue_jgpdzt.Properties.DataSource = jieguoList;
                    }
                    else
                    {
                     
                        layoutCon.Visibility = LayoutVisibility.Always;
                        layoutMin.Visibility = LayoutVisibility.Never;
                        layoutMax.Visibility = LayoutVisibility.Never;
                        var jieguoList = ResultJudgementStateHelper.GetResultTexStateModels();
                        lue_jgpdzt.Properties.DataSource = jieguoList;
                    }
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(lookUpitem.EditValue?.ToString()))
            {
                MessageBox.Show("请选择项目");
                return;
            }
             if (string.IsNullOrEmpty(lue_jgpdzt.EditValue?.ToString()))
            {
                MessageBox.Show("请选择判断标准");
                return;
            }
            VisitManageSetDto visitManageSetDto = new VisitManageSetDto();
                if (!string.IsNullOrEmpty(lookUpitem.Tag.ToString()))
            { visitManageSetDto.Id = (Guid)lookUpitem.Tag; }
            visitManageSetDto.ItemId= (Guid)lookUpitem.EditValue ;
            visitManageSetDto.MinAge= (int)num_zxnl.EditValue;
            visitManageSetDto.MaxAge= (int)num_zdnl.EditValue ;
            visitManageSetDto.Sex= lue_xingbie.EditValue.ToString();
            visitManageSetDto.CheckType= (int)lue_jgpdzt.EditValue;
            if (string.IsNullOrEmpty(num_zxz.EditValue?.ToString()))
            {
                visitManageSetDto.MinValue = (int)num_zxz.EditValue;
            }
            if (string.IsNullOrEmpty(num_zdz.EditValue?.ToString()))
            {
                visitManageSetDto.MaxValue = (int)num_zdz.EditValue;
            }
            if (string.IsNullOrEmpty(spinEdit1.EditValue?.ToString()))
            {
                visitManageSetDto.VisitDay = (int)spinEdit1.EditValue;
            }


            visitManageSetDto.Summ = num_zdz.EditValue.ToString();
            if (string.IsNullOrEmpty(lookUpEdit2.EditValue?.ToString()))
            {
                visitManageSetDto.Visitgrade = (int)lookUpEdit2.EditValue;
            }

            visitManageSetDto.VisitText = txtZQ.EditValue.ToString();
            visitManageSetDto.VisitName = textEditName.EditValue.ToString();
            if (string.IsNullOrEmpty(checkedComYCZT.EditValue?.ToString()))
            {
                visitManageSetDto.Symbol = checkedComYCZT.EditValue.ToString();
            }
           
            visitManageSetDto.VisitType = (int)radioGroup1.EditValue;
           itemInfoService.SaveVisitManageSet(visitManageSetDto);

            var lsit = itemInfoService.showVisitManages();
            gridControl1.DataSource = lsit;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            lookUpitem.EditValue = "";
            num_zxnl.EditValue = "";
            num_zdnl.EditValue = "";
            lue_xingbie.EditValue = "";
            lue_jgpdzt.EditValue = "";
            num_zxz.EditValue = "";
            spinEdit1.EditValue = "";
            num_zxz.EditValue = "";
            num_zdz.EditValue = "";
            lookUpEdit2.EditValue = "";
            textEditName.EditValue = "";
            radioGroup1.EditValue = "";

            checkedComYCZT.EditValue = 0;

            txtZQ.EditValue = "";
        }

        private void frmCriManage_Shown(object sender, EventArgs e)
        {
            var lsit = itemInfoService.showVisitManages();
            gridControl1.DataSource = lsit;

        }

        private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            var dto = gridControl1.GetFocusedRowDto<ShowVisitManageSetDto>();
            if (dto != null)
            {
                lookUpitem.EditValue = dto.ItemId;
                num_zxnl.EditValue = dto.MinAge;
                num_zdnl.EditValue = dto.MaxAge;
                lue_xingbie.EditValue = dto.Sex;
                lue_jgpdzt.EditValue = dto.CheckType;
                num_zxz.EditValue = dto.Summ;
                spinEdit1.EditValue = dto.VisitDay;
                num_zxz.EditValue = dto.MinValue;
                num_zdz.EditValue = dto.MaxValue;
                lookUpEdit2.EditValue = dto.VisitDay;
                lookUpitem.Tag = dto.Id;
                textEditName.EditValue = dto.VisitName;
                radioGroup1.EditValue = dto.VisitType;

                checkedComYCZT.EditValue = dto.Symbol;

                txtZQ.EditValue = dto.VisitText;

            }
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(lookUpitem.Tag?.ToString()))
            {
                MessageBox.Show("请选择项目");
                return;
            }

            VisitManageSetDto visitManageSet = new VisitManageSetDto();
            visitManageSet.Id = (Guid)lookUpitem.Tag;
              itemInfoService.DelVisitManageSet(visitManageSet);
            var lsit = itemInfoService.showVisitManages();
            gridControl1.DataSource = lsit;
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroup1.EditValue.ToString() == "0")
            {
                layoutYCZT.Visibility = LayoutVisibility.Always;
                layoutJGBZ.Visibility = LayoutVisibility.Never;
            }
            else
            {
                layoutYCZT.Visibility = LayoutVisibility.Never;
                layoutJGBZ.Visibility = LayoutVisibility.Always;
            }


                                       
        }
    }
}
